package com.seedwill.common.query.schema;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

public class QueryCriteriaGroupManager {
	
	public static final String MAIN = "_MAIN_"; //$NON-NLS-1$
	/**
	 * 条件组集合
	 */
	private Map<String, QueryCriteriaGroup> criteriaGroups;
 
	/**
	 * 初始化
	 */
	protected void checkInitCriteriaGroup(){
		if(null == criteriaGroups){
			criteriaGroups = new TreeMap<String, QueryCriteriaGroup>();
		}
	}
	/**
	 * 增加条件组
	 */
	public void addCriteriaGroup(String criteriaKey, QueryCriteriaGroup criteria){
		checkInitCriteriaGroup();
		if(null == criteriaKey || criteriaKey.equals(MAIN)){
			throw new AddCriteriaGroupException("invalid criteria group : NULL or invalid key["+MAIN+"]"); //$NON-NLS-1$
		}else{
			criteriaGroups.put(criteriaKey, criteria);
		}
		
	}
	/**
	 * 删除条件组
	 * @param criteriaKey
	 * @throws RemoveEelementException
	 */
	public void removeCriteriaGroup(String criteriaKey) throws RemoveCriteriaGroupException {
		checkInitCriteriaGroup();
		if(criteriaGroups.containsKey(criteriaKey)){
			criteriaGroups.remove(criteriaKey);
		}else{
			throw new RemoveCriteriaGroupException("no such element to remove:"+criteriaKey); //$NON-NLS-1$
		}
	}
	/**
	 * 清除条件组
	 * @throws CleanEelementException
	 */
	public void cleanCriteriaGroup() throws CleanCriteriaGroupException {
		if(null != criteriaGroups){
			criteriaGroups.clear();
		}	
	}
	
	/**
	 * 取得条件组
	 * @return
	 */
	public Collection<QueryCriteriaGroup> getCriteriaGroupList(){
		return null == criteriaGroups? null : criteriaGroups.values();
	}
}
